package num169;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/10 2:04
 * @Desc :
 */
public class Solution {
  public int majorityElement(int[] nums) {
    int candidate = nums[0];
    int count = 1;
    for (int i = 1; i < nums.length; i++) {
      if (count == 0) candidate = nums[i];
      count += nums[i] == candidate ? 1 : -1;
    }
    return candidate;
  }

  public static void main(String[] args) {
    int res = new Solution().majorityElement(new int[]{2, 2, 1, 1, 1, 2, 2});
    System.out.println(res);
  }
}
